package learnjava.basic.other;

import java.io.*;

public class TestSerilizable {

    public static void main(String[] args) throws Exception{

        String filePath = "e:\\data.dat";
        //序列化数据到filePath
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
        oos.writeInt(100); //int->Integer自动装箱，而Integer已经实现了Serializable
        oos.writeBoolean(true);
        oos.writeChar('a');
        oos.writeDouble(9.5);
        oos.writeUTF("韩顺平");
        oos.writeObject(new Dog("旺财",1));
        System.out.println("序列化完毕");

        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
        System.out.println(ois.readInt());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());
        Dog dog = (Dog) ois.readObject();
        System.out.println(dog.age+","+dog.name);
    }
}

class Dog implements Serializable{
    String name;
    int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
